package com.ydc.ln.tutorial.gui.app.minieditor;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;

/**
 * 实现文字字体、字号的编辑功能
 * 
 * @author ydc
 * @date 上午12:59:44 2013-4-13
 */
public class MenuFont extends JDialog
        implements
            ItemListener,
            ActionListener,
            TextListener {

    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = -4513916983946856553L;

    // AWT组件声明
    CheckboxGroup gp;
    Checkbox plain, bold, italic, boldItalic;
    Choice fontNameChoice;
    TextField fontSizeTextField;
    List fontSizeList;
    TextField fontTextField;
    Button fontButtonOK;
    Button fontButtonCancel;

    // 默认字体
    int fontStyleInt = 0;

    // 更改标记
    boolean changed = true;

    // 字号范围
    int fontSizeMin = 10, fontSizeMax = 36, fontSizeChangedStep = 2;

    // 构造方法
    MenuFont(Frame frame, boolean modal) {
        super(frame, modal);
    }

    // myLayout方法，窗体布局
    public Font myLayout(Font taFont) {
        this.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;

        // 初始化字号文本框
        fontSizeTextField = new TextField(taFont.getSize() + "", 14);
        fontSizeTextField.selectAll();
        fontSizeTextField.addTextListener(this);
        gbc.gridx = 0;
        gbc.gridy = 1;
        getContentPane().add(fontSizeTextField, gbc);

        // 初始化字体复选框
        gp = new CheckboxGroup();
        if (taFont.getStyle() == 0) {
            plain = new Checkbox("PLAIN", true, gp);
        } else {
            plain = new Checkbox("PLAIN", false, gp);
        }
        plain.addItemListener(this);

        if (taFont.getStyle() == 1) {
            bold = new Checkbox("BOLD", true, gp);
        } else {
            bold = new Checkbox("BOLD", false, gp);
        }
        bold.addItemListener(this);

        if (taFont.getStyle() == 2) {
            italic = new Checkbox("ITALIC", true, gp);
        } else {
            italic = new Checkbox("ITALIC", false, gp);
        }
        italic.addItemListener(this);

        if (taFont.getStyle() == 3) {
            boldItalic = new Checkbox("BOLDITALIC", true, gp);
        } else {
            boldItalic = new Checkbox("BOLDITALIC", false, gp);
        }
        boldItalic.addItemListener(this);

        gbc.gridx = 0;
        gbc.gridy = 0;
        getContentPane().add(plain, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        getContentPane().add(bold, gbc);
        gbc.gridx = 2;
        gbc.gridy = 0;
        getContentPane().add(italic, gbc);
        gbc.gridx = 3;
        gbc.gridy = 0;
        getContentPane().add(boldItalic, gbc);

        // 初始化字号列表
        fontSizeList = new List(4, false);
        fontSizeList.addItemListener(this);
        int indexOfList = 0;
        boolean breakHere = false;
        for (int i = fontSizeMin; i <= fontSizeMax; i = i + fontSizeChangedStep) {
            if (!breakHere) {
                if (i != taFont.getSize()) {
                    indexOfList++;
                } else {
                    breakHere = true;
                }
            }
            fontSizeList.add(String.valueOf(i));
        }
        if (indexOfList < 0
                || indexOfList > (fontSizeMax - fontSizeMin)
                        / fontSizeChangedStep + 1) {
            fontSizeList.select(0);
        } else {
            fontSizeList.select(indexOfList);
        }
        gbc.gridx = 0;
        gbc.gridy = 2;
        getContentPane().add(fontSizeList, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        fontNameChoice = new Choice();
        fontNameChoice.addItemListener(this);
        fontNameChoice.add("Serif");
        fontNameChoice.add("Courier");
        fontNameChoice.add("Helvetica");
        fontNameChoice.add("timesRoman");
        fontNameChoice.select(taFont.getName());
        getContentPane().add(fontNameChoice, gbc);

        // 初始化OK按钮
        fontButtonOK = new Button("OK");
        fontButtonOK.addActionListener(this);
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        getContentPane().add(fontButtonOK, gbc);

        // 初始化Cancel按钮
        fontButtonCancel = new Button("Cancel");
        fontButtonCancel.addActionListener(this);
        gbc.gridx = 3;
        gbc.gridy = 2;
        getContentPane().add(fontButtonCancel, gbc);

        // 初始化字体选择预览文本框
        fontTextField = new TextField("Java awt");
        fontTextField.setEditable(false);
        fontTextField.setSize(90, 60);
        fontTextField.setFont(taFont);
        gbc.weightx = 100;
        gbc.weighty = 230;
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        getContentPane().add(fontTextField, gbc);

        // 添加窗体监听器
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });

        this.setLocation(120, 120);
        this.setResizable(false);
        this.setSize(480, 160);

        // 显示窗体
        this.setVisible(true);
        if (changed) {
            return returnFont();
        } else {
            return taFont;
        }

    }


    // 文本改变监听方法
    @Override
    public void textValueChanged(TextEvent e) {
        int indexOfList = 0;
        boolean breakHere = false;
        int thisNum = 0;
        for (int i = fontSizeMin; i <= fontSizeMax; i = i + fontSizeChangedStep) {
            if (!breakHere) {
                try {
                    thisNum = Integer.parseInt(fontSizeTextField.getText());
                } catch (NumberFormatException nfe) {
                    thisNum = 0;
                }
                if (i != thisNum) {
                    indexOfList++;
                } else {
                    breakHere = true;
                }
            }
        }
        if (indexOfList < 0
                || indexOfList > (fontSizeMax - fontSizeMin)
                        / fontSizeChangedStep + 1) {
            fontSizeList.select(0);
        } else {
            fontSizeList.select(indexOfList);
        }
        updateFontTextField();
    }

    // 事件监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == fontButtonOK) {
            changed = true;
            dispose();
        } else if (e.getSource() == fontButtonCancel) {
            changed = false;
            dispose();
        }
    }

    // Item监听方法
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == fontSizeList) {
            List lf = (List) e.getSource();
            fontSizeTextField.setText(lf.getSelectedItem());
            updateFontTextField();
        } else if (e.getSource() == plain || e.getSource() == bold
                || e.getSource() == italic || e.getSource() == boldItalic
                || e.getSource() == fontNameChoice) {
            updateFontTextField();
        }
    }

    /**
     * 返回当前字体
     * 
     * @return Font
     */
    public Font returnFont() {
        updateFontTextField();
        return new Font(fontNameChoice.getSelectedItem(), fontStyleInt,
                Integer.parseInt(fontSizeList.getSelectedItem()));
    }

    /**
     * 更新字体 void
     */
    private void updateFontTextField() {
        String fontStyleStr = gp.getSelectedCheckbox().getLabel();
        if (fontStyleStr.equals("PLAIN")) {
            fontStyleInt = Font.PLAIN;
        } else if (fontStyleStr.equals("BOLD")) {
            fontStyleInt = Font.BOLD;
        } else if (fontStyleStr.equals("ITALIC")) {
            fontStyleInt = Font.ITALIC;
        } else if (fontStyleStr.equals("BOLDITALIC")) {
            fontStyleInt = Font.BOLD + Font.ITALIC;
        }
        fontTextField
                .setFont(new Font(fontNameChoice.getSelectedItem(),
                        fontStyleInt, Integer.parseInt(fontSizeList
                                .getSelectedItem())));
    }

}
